package com.kzj.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kzj.dao.StudentClassDao;
import com.kzj.dao.StudentDao;
import com.kzj.domain.Major;
import com.kzj.domain.StudentClass;
import com.kzj.domain.vo.StudentClassVO;
import com.kzj.global.constant.GlobalConstant;
import com.kzj.global.re.Result;
import com.kzj.global.utils.GlobalUtil;
import com.kzj.service.MajorService;
import com.kzj.service.StudentClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class StudentClassServiceImpl extends ServiceImpl<StudentClassDao, StudentClass> implements StudentClassService
{
    @Autowired
    MajorService majorService;
    @Override
    public String getClassNameByStudentClass(StudentClass studentClass) {
        Major major = majorService.getById(studentClass.getMajorId());
        String s = GlobalUtil.addZero(studentClass.getSerialNumber(), GlobalConstant.STUDENT_CLASS_SERIAL_NUMBER_LEN);
        return major.getName()+" "+(studentClass.getPeriod()%100)+s;
    }

    @Override
    public String getClassNameBYStudentClassId(String studentClassId) {
        StudentClass studentClass = this.getById(studentClassId);
       return getClassNameByStudentClass(studentClass);
    }

    @Override
    public Result getByMajorIdAndPeriod(String majorId,Integer period) {
        List<StudentClass> list = this.query().eq(majorId != null || majorId.equals(""), "major_id", majorId).eq(period != null, "period", period).list();
        List<StudentClassVO> studentClassVOS = list.stream().map(item -> {
            StudentClassVO studentClassVO = new StudentClassVO();
            studentClassVO.setId(item.getId());
            studentClassVO.setName(this.getClassNameByStudentClass(item));
            return studentClassVO;
        }).collect(Collectors.toList());
        return Result.success(studentClassVOS);
    }
}
